/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session03;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 *
 * @author macbookpro
 */
public class GZIPCompressUncompressTest {

    public static void main(String[] args) throws Exception {

        FileInputStream fis = new FileInputStream("text file.txt");
        FileOutputStream fos = new FileOutputStream("compressed.gz");
        GZIPOutputStream gos = new GZIPOutputStream(fos);

        doCopy(fis, gos); // copy and compress

        FileInputStream fis2 = new FileInputStream("compressed.gz");
        GZIPInputStream gis = new GZIPInputStream(fis2);
        FileOutputStream fos2 = new FileOutputStream("uncompressed.txt");

        doCopy(gis, fos2); // copy and uncompress

    }

    public static void doCopy(InputStream is, OutputStream os) throws Exception {
        int oneByte;
        while ((oneByte = is.read()) != -1) {
            os.write(oneByte);
        }
        os.close();
        is.close();
    }
}
